﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

using BaseCardTypes = Wormhole.Engine.CardLibrary.BaseCardTypes;

namespace Wormhole.UI.Application.Controls.Status
{
	/// <author>Hannes Foulds</author>
	/// <date>19 June 2008</date>
	/// <summary>
	/// Display the card status.
	/// </summary>
	public class CardStatus : Grid
	{
		#region Declarations
		/// <summary>The hand icon image source.</summary>
		protected ImageSource handSource;

		/// <summary>The fist icon image source.</summary>
		protected ImageSource fistSource;
		#endregion
		
		#region User Interface Elements
		/// <summary>The card status image to display.</summary>
		protected Image statusImage;
		#endregion

		#region Properties
		/// <summary>
		/// The card to reender the status for.
		/// </summary>
		public BaseCardTypes.SkillsCard EngineCard { get; set; }
		#endregion

		#region Constructor
		/// <summary>
		/// Create a default instance of the class.
		/// </summary>
		public CardStatus()
		{
			this.CreateChildControls();
			this.LoadImages();
		}

		public CardStatus(BaseCardTypes.SkillsCard card)
			: this()
		{
			this.EngineCard = card;
			this.EngineCard.StatusChanged += new Wormhole.Engine.CardLibrary.BaseCardTypes.SkillsCard.StatusChangedDelegate(EngineCard_StatusChanged);
			this.DisplayStatus();
		}
		#endregion

		#region Create Child Controls
		/// <summary>
		/// Create the child controls.
		/// </summary>
		protected virtual void CreateChildControls()
		{
			// create the status image
			this.statusImage = new Image();
			this.Children.Add(this.statusImage);
		}
		#endregion

		#region Event Handlers
		/// <summary>
		/// Handle the engine card status change.
		/// </summary>
		/// <param name="status">The new card status.</param>
		private void EngineCard_StatusChanged(Wormhole.Engine.CardLibrary.CardStatus status)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.DisplayStatus();
			});
		}
		#endregion

		#region Load Images
		/// <summary>
		/// Load the images that will be used to display the various card states.
		/// </summary>
		private void LoadImages()
		{
			this.fistSource = this.FindResource("CardStatus_Fist") as ImageSource;
			this.handSource = this.FindResource("CardStatus_Hand") as ImageSource;
		}
		#endregion

		#region Display Status
		/// <summary>
		/// Display the card status.
		/// </summary>
		private void DisplayStatus()
		{
			if (this.EngineCard != null)
			{
				switch (this.EngineCard.Status)
				{
					case Wormhole.Engine.CardLibrary.CardStatus.Stopped:
						this.statusImage.Source = this.handSource;
						break;
					case Wormhole.Engine.CardLibrary.CardStatus.Blocked:
						this.statusImage.Source = this.fistSource;
						break;
					default:
						this.statusImage.Source = null;
						break;
				}
			}
		}
		#endregion
	}
}
